import java.util.HashSet;
import java.util.List;

public class Solution127 {

    public int ladderLength(String beginWord, String endWord, List<String> wordList) {
        if (wordList == null || wordList.size() == 0) {
            return 0;
        }
        HashSet<String> start = new HashSet<>();
        HashSet<String> end = new HashSet<>();
        HashSet<String> dic = new HashSet<>(wordList);
        if (!dic.contains(endWord)) {
            return 0;
        }
        start.add(beginWord);
        end.add(endWord);
        int step = 1;
        while (!start.isEmpty()) {
            step++;
            HashSet<String> tmp = new HashSet<>();
            dic.removeAll(start);
            for (String s : start) {
                char[] arr = s.toCharArray();
                for (int i = 0; i < arr.length; i++) {
                    char c = arr[i];
                    for (char j = 'a'; j <= 'z'; j++) {
                        if (c == j) {
                            continue;
                        }
                        arr[i] = j;
                        String strTmp = new String(arr);
                        if (dic.contains(strTmp)) {
                            if (end.contains(strTmp)) {
                                return step;
                            } else {
                                tmp.add(strTmp);
                            }
                        }
                    }
                    arr[i] = c;
                }

            }
            if (tmp.size() < end.size()) {
                start.clear();
                start = tmp;
            } else {
                start.clear();
                start = end;
                end = tmp;
            }
        }
        return 0;
    }
/*
    boolean[][] graph;
    boolean[] visited;
    int res;

    public int ladderLength(String beginWord, String endWord, List<String> wordList) {
        int n = wordList.size();
        graph = new boolean[n][n];
        visited = new boolean[n];
        res = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (helper(wordList.get(i), wordList.get(j))) {
                    graph[j][i] = graph[i][j] = true;
                }
            }
        }
        int end = n;
        for (int i = 0; i < n; i++) {
            if (endWord.equals(wordList.get(i))) {
                end = i;
                break;
            }
        }
        if (end == n) {
            return 0;
        }
        for (int i = 0; i < n; i++) {
            if (helper(beginWord, wordList.get(i))) {
                dfs(i, end, 2);
            }
        }
        return res;
    }

    private boolean helper(String s, String t) {
        int n = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) != t.charAt(i)) {
                n++;
                if (n > 1) {
                    return false;
                }
            }
        }
        return true;
    }

    private void dfs(int start, int end, int len) {
        if (start == end) {
            if (res == 0) {
                res = len;
            } else {
                res = Math.min(res, len);
            }
            return;
        }
        for (int i = 0; i < graph.length; i++) {
            if (graph[start][i] && !visited[i]) {
                visited[i] = true;
                dfs(i, end, len + 1);
                visited[i] = false;
            }
        }
    }*/
}
